var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
(function (factory) {
    if (typeof module === "object" && typeof module.exports === "object") {
        var v = factory(require, exports);
        if (v !== undefined) module.exports = v;
    }
    else if (typeof define === "function" && define.amd) {
        define(["require", "exports", "react", "controls/base-page", "controls/item-dialog", "services/activity", "controls/product-select-dialog", "services/shopping", "maishu-ui-toolkit", "maishu-dilu", "site", "maishu-chitu-service"], factory);
    }
})(function (require, exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", { value: true });
    const React = require("react");
    const base_page_1 = require("controls/base-page");
    const item_dialog_1 = require("controls/item-dialog");
    const activity_1 = require("services/activity");
    const product_select_dialog_1 = require("controls/product-select-dialog");
    const shopping_1 = require("services/shopping");
    const maishu_ui_toolkit_1 = require("maishu-ui-toolkit");
    const maishu_dilu_1 = require("maishu-dilu");
    const site_1 = require("site");
    const maishu_chitu_service_1 = require("maishu-chitu-service");
    const promotionTypeTexts = {
        Reduce: '满减',
        Given: '满赠',
        Discount: '满折'
    };
    const promotionMethodTexts = {
        Amount: '按商品总金额',
        Count: '按商品总数量'
    };
    const objectTypeTexts = {
        Brand: '品牌',
        Category: '类别',
        Product: '商品'
    };
    const promotionTypes = {
        amount: 'Reduce',
        product: 'Given',
        discount: 'Discount'
    };
    const promotionMethods = {
        amount: 'Amount',
        count: 'Count'
    };
    let objectNames = {};
    class ActivityEditPage extends base_page_1.BasePage {
        constructor(props) {
            super(props);
            this.state = {};
            this.activityService = this.props.createService(activity_1.ActivityService);
        }
        newProductRule(promotion) {
            let rule = { ObjectType: "Product", PromotionId: promotion.Id, CollectionType: "Include" };
            this.productDialog.show(rule, "添加商品", (item) => {
                promotion.PromotionRangeRules.push(item);
                this.setState(this.state);
            });
        }
        newBrandRule(promotion) {
            let rule = { ObjectType: "Brand", PromotionId: promotion.Id, CollectionType: "Include" };
            this.brandDialog.show(rule, "添加品牌", (item) => {
                promotion.PromotionRangeRules.push(item);
                this.setState(this.state);
            });
        }
        newCategoryRule(promotion) {
            let rule = { ObjectType: "Category", PromotionId: promotion.Id, CollectionType: "Include" };
            this.categoryDialog.show(rule, "添加类别", (item) => {
                promotion.PromotionRangeRules.push(item);
                this.setState(this.state);
            });
        }
        /** 满减 */
        newBuyReduce(promotion, rule) {
            if (rule == null)
                rule = { PromotionId: promotion.Id };
            this.reduceDialog.show({ promotion, rule }, "满减");
        }
        /** 满折 */
        newBuyDiscount(promotion, rule) {
            if (rule == null)
                rule = { PromotionId: promotion.Id };
            this.discountDialog.show({ promotion, rule }, "满折");
        }
        /** 满赠 */
        newBuyGiven(promotion, rule) {
            if (rule == null)
                rule = { PromotionId: promotion.Id };
            this.givenDialog.show({ promotion, rule }, "满赠", ({ promotion, rule }) => {
                rule.Description = this.description(promotion, rule);
                if (!rule.Id) {
                    rule.Id = maishu_chitu_service_1.guid();
                    promotion.PromotionContentRules.push(rule);
                }
                this.setState(this.state);
            });
        }
        getConditionText(promotion, promotionRule) {
            var condition_text;
            if (promotion.Method == promotionMethods.count) {
                condition_text = `购买指定商品任意 ${promotionRule.LevelValue} 件, `;
            }
            else if (promotion.Method == promotionMethods.amount) {
                condition_text = `购买指定商品满 ￥${new Number(promotionRule.LevelValue).toFixed(2)} 元, `;
            }
            return condition_text;
        }
        description(promotion, contentRule) {
            if (promotion.Type == promotionTypes.product) {
                var str = '即可获赠'; // chitu.Utility
                let items = contentRule.GivenValue.split(",").map(o => {
                    let arr = o.split(":");
                    let id = arr[0];
                    let quantity = Number.parseInt(arr[1]);
                    return { id, quantity };
                });
                for (var i = 0; i < items.length; i++) {
                    if (i > 0)
                        str = str + "，";
                    str = str + ` “${objectNames[items[i].id]}” ${items[i].quantity} 件`;
                }
                let description = this.getConditionText(promotion, contentRule) + str;
                return description;
            }
            else if (promotion.Type == promotionTypes.amount) {
                let str = `￥${new Number(contentRule.GivenValue).toFixed(2)}`;
                var given_text = `即减 ${str} 元`;
                return this.getConditionText(promotion, contentRule) + given_text;
            }
            else if (promotion.Type == promotionTypes.discount) {
                var given_text = `打 ${contentRule.GivenValue} 折`;
                return this.getConditionText(promotion, contentRule) + given_text;
            }
        }
        showContentRuleDialog(promotion, rule) {
            switch (promotion.Type) {
                case promotionTypes.amount:
                    this.newBuyReduce(promotion, rule);
                    break;
                case promotionTypes.discount:
                    this.newBuyDiscount(promotion, rule);
                    break;
                case promotionTypes.product:
                    this.newBuyGiven(promotion, rule);
                    break;
            }
        }
        removeRangeRule(p, c) {
            p.PromotionRangeRules = p.PromotionRangeRules.filter(o => o.Id == c.Id);
            this.setState(this.state);
        }
        removeContentRule(p, c) {
            p.PromotionContentRules = p.PromotionContentRules.filter(o => o.Id == c.Id);
            this.setState(this.state);
        }
        componentDidMount() {
            this.activityService.promotions(this.props.data.id).then(r => {
                this.setState({ promotions: r });
            });
        }
        renderButtns() {
            return [
                React.createElement("button", { className: "btn btn-sm btn-primary" },
                    React.createElement("i", { className: "icon-save" }),
                    React.createElement("span", null, "\u4FDD\u5B58")),
                React.createElement("button", { className: "btn btn-sm btn-primary", onClick: () => this.promotionDialog.show({ Method: "Amount", Type: "Given" }, "添加优惠", (item) => {
                        let id = this.props.data.id;
                        return this.activityService.addPromotion(id, item.Type, item.Method);
                    }) },
                    React.createElement("i", { className: "icon-plus" }),
                    React.createElement("span", null, "\u6DFB\u52A0\u4F18\u60E0")),
            ];
        }
        renderView() {
            let { promotions } = this.state;
            if (promotions == null) {
                return site_1.default.loadingElement;
            }
            return React.createElement(React.Fragment, null,
                promotions.map(p => React.createElement(React.Fragment, { key: p.Id },
                    React.createElement("div", { className: "row" },
                        React.createElement("div", { className: "col-md-6" },
                            React.createElement("table", { className: "table table-striped table-bordered table-hover", style: { borderCollapse: "collapse" } },
                                React.createElement("thead", null,
                                    React.createElement("tr", null,
                                        React.createElement("th", { colSpan: 4 }, "\u4F18\u60E0\u8303\u56F4")),
                                    React.createElement("tr", null,
                                        React.createElement("th", { style: { textAlign: "center", width: 60 } }, "\u7C7B\u578B"),
                                        React.createElement("th", { style: { textAlign: "center" } }, "\u540D\u79F0"),
                                        React.createElement("th", { style: { textAlign: "center", width: 160 } }, "\u64CD\u4F5C"))),
                                React.createElement("tbody", null, p.PromotionRangeRules.length == 0 ?
                                    React.createElement("tr", { "data-bind": "visible:ko.unwrap(rules).length == 0" },
                                        React.createElement("td", { colSpan: 4, style: { border: 0, textAlign: "center" } },
                                            React.createElement("div", { style: { padding: "50px 0px 50px 0px" } },
                                                React.createElement("b", null, "\u6682\u65E0\u4F18\u60E0\u5546\u54C1"),
                                                React.createElement("div", null, "\u53EF\u4EE5\u52FE\u9009\"\u5168\u573A\u4F18\u60E0\"\u6216\u8005\u70B9\u51FB\"\u6DFB\u52A0\u5546\u54C1\"\u6309\u94AE\u6DFB\u52A0\u8981\u4F18\u60E0\u7684\u5546\u54C1"))))
                                    : p.PromotionRangeRules.map(c => React.createElement("tr", { key: c.Id },
                                        React.createElement("td", null, objectTypeTexts[c.ObjectType]),
                                        React.createElement("td", null, c.ObjectName),
                                        React.createElement("td", { className: "text-center" },
                                            React.createElement("label", { className: "switch" },
                                                React.createElement("input", { type: "checkbox", className: "ace ace-switch ace-switch-5", checked: c.CollectionType == "Include", onChange: e => {
                                                        c.CollectionType = e.target.checked ? "Include" : "Exclude";
                                                        this.setState({ promotions });
                                                    } }),
                                                React.createElement("span", { className: "lbl middle" })),
                                            React.createElement("button", { className: "btn btn-minier btn-danger", style: { marginLeft: 4 }, ref: e => {
                                                    maishu_ui_toolkit_1.buttonOnClick(e, () => __awaiter(this, void 0, void 0, function* () { return this.removeRangeRule(p, c); }), { confirm: `是否删除 ${c.ObjectName} 吗` });
                                                } },
                                                React.createElement("i", { className: "icon-trash" })))))),
                                React.createElement("tfoot", null,
                                    React.createElement("tr", null,
                                        React.createElement("td", { colSpan: 3, className: "" },
                                            React.createElement("div", { className: "pull-left" },
                                                React.createElement("label", null,
                                                    React.createElement("input", { "data-bind": "checked:allProducts", type: "checkbox", checked: p.IsAll, onChange: e => {
                                                            p.IsAll = e.target.checked;
                                                            this.setState({ promotions });
                                                        } }),
                                                    "\u00A0 \u5168\u573A\u4F18\u60E0")),
                                            React.createElement("div", { className: "pull-right" },
                                                React.createElement("button", { className: "btn btn-primary btn-sm", onClick: () => this.newProductRule(p) }, "\u6DFB\u52A0\u5546\u54C1"),
                                                React.createElement("button", { className: "btn btn-primary btn-sm", onClick: () => this.newBrandRule(p) }, "\u6DFB\u52A0\u54C1\u724C"),
                                                React.createElement("button", { className: "btn btn-primary btn-sm", onClick: () => this.newCategoryRule(p) }, "\u6DFB\u52A0\u54C1\u7C7B"))))))),
                        React.createElement("div", { className: "col-md-6" },
                            React.createElement("table", { className: "table table-striped table-bordered table-hover", style: { borderCollapse: "collapse" } },
                                React.createElement("thead", null,
                                    React.createElement("tr", null,
                                        React.createElement("th", { colSpan: 4 }, "\u4F18\u60E0\u5185\u5BB9")),
                                    React.createElement("tr", null,
                                        React.createElement("th", { style: { textAlign: "center" } }, "\u540D\u79F0"),
                                        React.createElement("th", { style: { textAlign: "center", width: 100 } },
                                            React.createElement("span", null, p.Method == "Amount" ? "金额" : "数量")),
                                        React.createElement("th", { style: { textAlign: "center", width: 80 } }, "\u64CD\u4F5C"))),
                                React.createElement("tbody", null, p.PromotionContentRules.length == 0 ?
                                    React.createElement("tr", { "data-bind": "visible:ko.unwrap(rules).length==0" },
                                        React.createElement("td", { colSpan: 4, style: { border: 0, textAlign: "center" } },
                                            React.createElement("div", { style: { padding: "50px 0px 50px 0px" } },
                                                React.createElement("b", null, "\u6682\u65E0\u4F18\u60E0\u5185\u5BB9"),
                                                React.createElement("div", null, "\u53EF\u4EE5\u70B9\u51FB\"\u6DFB\u52A0\u4F18\u60E0\u5185\u5BB9\"\u6309\u94AE,\u6DFB\u52A0\u4F18\u60E0\u5185\u5BB9")))) :
                                    p.PromotionContentRules.map(c => React.createElement("tr", { key: c.Id },
                                        React.createElement("td", null, c.Description),
                                        React.createElement("td", { style: { textAlign: "right" } }, p.Method == "Amount" ? "￥" + c.LevelValue.toFixed(2) : c.LevelValue),
                                        React.createElement("td", { style: { textAlign: "center" } },
                                            React.createElement("button", { className: "btn btn-minier btn-danger", onClick: e => this.removeContentRule(p, c), ref: e => {
                                                    if (!e)
                                                        return;
                                                    maishu_ui_toolkit_1.buttonOnClick(e, () => __awaiter(this, void 0, void 0, function* () { return this.removeContentRule(p, c); }), { confirm: `确定删除 '${c.Description}' 吗` });
                                                } },
                                                React.createElement("i", { className: "icon-trash" })))))),
                                React.createElement("tfoot", null,
                                    React.createElement("tr", null,
                                        React.createElement("td", { colSpan: 4 },
                                            React.createElement("div", { className: "pull-left" },
                                                React.createElement("div", null,
                                                    React.createElement("label", null,
                                                        React.createElement("span", { style: { fontWeight: 'bold' } }, "\u4F18\u60E0\u65B9\u5F0F\uFF1A"),
                                                        React.createElement("span", { "data-bind": "text:methodText" }, promotionMethodTexts[p.Method])),
                                                    React.createElement("label", { style: { paddingLeft: 10 } },
                                                        React.createElement("span", { style: { fontWeight: 'bold' } }, "\u4F18\u60E0\u7C7B\u578B\uFF1A"),
                                                        React.createElement("span", { "data-bind": "text:typeText" }, promotionTypeTexts[p.Type])))),
                                            React.createElement("div", { className: "pull-right" },
                                                React.createElement("button", { className: "btn btn-primary btn-sm", onClick: () => this.showContentRuleDialog(p) }, "\u6DFB\u52A0\u4F18\u60E0\u5185\u5BB9")))))))))),
                React.createElement(PromotionDialog, { ref: e => this.promotionDialog = e || this.promotionDialog }),
                React.createElement(ProductDialog, { ref: e => this.productDialog = e || this.productDialog }),
                React.createElement(BrandDialog, Object.assign({}, this.props, { ref: e => this.brandDialog = e || this.brandDialog })),
                React.createElement(CategoryDialog, Object.assign({}, this.props, { ref: e => this.categoryDialog = e || this.categoryDialog })),
                React.createElement(BuyGivenDialog, { ref: e => this.givenDialog = e || this.givenDialog }),
                React.createElement(BuyReduceDialog, { ref: e => this.reduceDialog = e || this.reduceDialog }),
                React.createElement(BuyDiscountDialog, { ref: e => this.discountDialog = e || this.discountDialog }));
        }
    }
    exports.default = ActivityEditPage;
    class PromotionDialog extends item_dialog_1.ItemDialog {
        constructor(props) {
            super(props);
        }
        render() {
            let item = this.state.item;
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u4F18\u60E0\u65B9\u5F0F"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "promotion_type", type: "radio", checked: item.Method == "Amount", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.Method = "Amount";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6309\u91D1\u989D\u4F18\u60E0")),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "promotion_type", type: "radio", checked: item.Method == "Count", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.Method = "Count";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6309\u8D2D\u4E70\u5546\u54C1\u603B\u6570")))),
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u4F18\u60E0\u7C7B\u578B"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "promotion_method", type: "radio", checked: item.Type == "Given", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.Type = "Given";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6EE1\u8D60")),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "promotion_method", type: "radio", checked: item.Type == "Reduce", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.Type = "Reduce";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6EE1\u51CF")),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "promotion_method", type: "radio", checked: item.Type == "Discount", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.Type = "Discount";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6EE1\u6298")))));
        }
    }
    class ProductDialog extends item_dialog_1.ItemDialog {
        constructor(props) {
            super(props);
            this.confirmButtonText = "确定";
        }
        render() {
            let item = this.state.item;
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u5546\u54C1\u540D\u79F0"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("div", { className: "input-group" },
                            React.createElement("input", { className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u4EA7\u54C1\u7F16\u53F7", style: { display: "none" }, value: item.ObjectId || "", onChange: e => {
                                    item.ObjectId = e.target.value;
                                    this.setState({ item });
                                } }),
                            React.createElement("input", { className: "form-control", placeholder: "\u8BF7\u9009\u62E9\u5546\u54C1", value: item.ObjectName || "", readOnly: true }),
                            React.createElement("span", { className: "input-group-btn" },
                                React.createElement("button", { name: "btnSelectProduct", className: "btn btn-default", type: "button", title: "\u70B9\u51FB\u9009\u62E9\u5546\u54C1", onClick: () => product_select_dialog_1.ProductSelectDialog.show((items) => {
                                        item.ObjectId = items[0].Id;
                                        item.ObjectName = items[0].Name;
                                        this.setState({ item });
                                    }) },
                                    React.createElement("i", { className: "icon-cog" })))))),
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("div", { className: "col-sm-10 col-sm-offset-2" },
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Include", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Include";
                                    this.setState({ item });
                                } }),
                            "\u5305\u542B"),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Exclude", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Exclude";
                                    this.setState({ item });
                                } }),
                            "\u6392\u9664"))));
        }
    }
    class BrandDialog extends item_dialog_1.ItemDialog {
        constructor(props) {
            super(props);
            this.OBJECT_ID = "ObjectId";
            this.confirmButtonText = "确定";
            this.shoppingService = this.props.createService(shopping_1.ShoppingService);
        }
        componentDidMount() {
            this.shoppingService.brands().then(r => {
                this.setState({ brands: r.dataItems });
            });
            this.validator = new maishu_dilu_1.FormValidator(this.element, { name: this.OBJECT_ID, rules: [maishu_dilu_1.rules.required("请选择品牌")] });
        }
        render() {
            let { item, brands } = this.state;
            brands = brands || [];
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u54C1\u724C"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("select", { name: this.OBJECT_ID, className: "form-control", value: item.ObjectId || "", onChange: e => {
                                if (e.target.value) {
                                    item.ObjectId = e.target.value;
                                    item.ObjectName = brands.filter(o => o.Id == e.target.value)[0].Name;
                                }
                                else {
                                    item.ObjectId = "";
                                    item.ObjectName = "";
                                }
                                this.setState({ item });
                            } },
                            React.createElement("option", { value: "" }, "\u8BF7\u9009\u62E9\u54C1\u724C"),
                            brands.map(c => React.createElement("option", { key: c.Id, value: c.Id }, c.Name))))),
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("div", { className: "col-sm-10 col-sm-offset-2" },
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Include", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Include";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u5305\u542B")),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Exclude", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Exclude";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6392\u9664")))));
        }
    }
    class CategoryDialog extends item_dialog_1.ItemDialog {
        constructor(props) {
            super(props);
            this.confirmButtonText = "确定";
            this.shoppingService = this.props.createService(shopping_1.ShoppingService);
        }
        componentDidMount() {
            this.shoppingService.categories().then(r => {
                this.setState({ categories: r });
            });
        }
        render() {
            let { item, categories } = this.state;
            categories = categories || [];
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u7C7B\u522B"),
                    React.createElement("div", { className: "col-sm-10" },
                        React.createElement("select", { className: "form-control", value: item.ObjectId || "", onChange: e => {
                                if (e.target.value) {
                                    item.ObjectId = e.target.value;
                                    item.ObjectName = categories.filter(o => o.Id == e.target.value)[0].Name;
                                }
                                else {
                                    item.ObjectId = "";
                                    item.ObjectName = "";
                                }
                                this.setState({ item });
                            } },
                            React.createElement("option", { value: "" }, "\u8BF7\u9009\u62E9\u7C7B\u522B"),
                            categories.map(c => React.createElement("option", { key: c.Id, value: c.Id }, c.Name))))),
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("div", { className: "col-sm-10 col-sm-offset-2" },
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Include", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Include";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u5305\u542B")),
                        React.createElement("label", { className: "radio-inline" },
                            React.createElement("input", { name: "productType", type: "radio", checked: item.CollectionType == "Exclude", onChange: e => {
                                    if (!e.target.checked)
                                        return;
                                    item.CollectionType = "Exclude";
                                    this.setState({ item });
                                } }),
                            React.createElement("span", null, "\u6392\u9664")))));
        }
    }
    class ContentRuleDialog extends item_dialog_1.ItemDialog {
        constructor(props) {
            super(props);
            this.LEVEL_VALUE = "LevelValue";
            this.confirmButtonText = "确定";
        }
        componentDidMount() {
            this.validator = new maishu_dilu_1.FormValidator(this.element, { name: this.LEVEL_VALUE, rules: [maishu_dilu_1.rules.required(() => this.state.item.promotion.Method == "Count" ? "请选择购买数量" : "请输入消费金额")] });
        }
        render() {
            let item = this.state.item;
            if (item.promotion == null || item.rule == null) {
                return site_1.default.loadingElement;
            }
            let method = item.promotion.Method;
            let rule = item.rule;
            return React.createElement(React.Fragment, null,
                method == "Count" ?
                    React.createElement("div", { className: "form-group clearfix", style: { display: method == "Count" ? null : 'none' } },
                        React.createElement("label", { className: "control-label col-sm-2" }, "*\u8D2D\u4E70\u6570\u91CF"),
                        React.createElement("div", { className: "col-sm-10" },
                            React.createElement("select", { name: this.LEVEL_VALUE, className: "form-control", value: rule.LevelValue, onChange: e => {
                                    rule.LevelValue = Number.parseInt(e.target.value);
                                    this.setState({ item });
                                } },
                                React.createElement("option", { value: "" }, "\u8BF7\u9009\u62E9\u8D2D\u4E70\u6570\u91CF"),
                                React.createElement("option", { value: 1 }, "\u4EFB\u610F1\u4EF6"),
                                React.createElement("option", { value: 2 }, "\u4EFB\u610F2\u4EF6"),
                                React.createElement("option", { value: 3 }, "\u4EFB\u610F3\u4EF6"),
                                React.createElement("option", { value: 4 }, "\u4EFB\u610F4\u4EF6"),
                                React.createElement("option", { value: 5 }, "\u4EFB\u610F5\u4EF6"),
                                React.createElement("option", { value: 6 }, "\u4EFB\u610F6\u4EF6"),
                                React.createElement("option", { value: 7 }, "\u4EFB\u610F7\u4EF6"),
                                React.createElement("option", { value: 8 }, "\u4EFB\u610F8\u4EF6")))) :
                    React.createElement("div", { className: "form-group clearfix", style: { display: method == "Amount" ? null : 'none' } },
                        React.createElement("label", { className: "control-label col-sm-2" }, "*\u6D88\u8D39\u91D1\u989D"),
                        React.createElement("div", { className: "col-sm-10" },
                            React.createElement("div", { className: "input-group" },
                                React.createElement("input", { name: this.LEVEL_VALUE, className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u6D88\u8D39\u91D1\u989D", value: rule.LevelValue || "", onChange: e => {
                                        rule.LevelValue = Number.parseFloat(e.target.value);
                                        this.setState({ item });
                                    } }),
                                React.createElement("div", { className: "input-group-addon" }, "\u5143")),
                            React.createElement("span", { className: `${maishu_dilu_1.FormValidator.errorClassName} ${this.LEVEL_VALUE}`, style: { display: "none" } }))),
                this.renderGiven());
        }
    }
    /** 满赠优惠对话框 */
    class BuyGivenDialog extends ContentRuleDialog {
        constructor() {
            super(...arguments);
            this.GIVEN_VALUE = "GivenValue";
        }
        componentDidMount() {
            super.componentDidMount();
            this.validator.appendField({ name: this.GIVEN_VALUE, rules: [maishu_dilu_1.rules.required("最少选择一个商品")] });
        }
        renderGiven() {
            let { item } = this.state;
            let givenProducts = (item.rule.GivenValue || "").split(",").filter(o => o).map(o => {
                let arr = o.split(":");
                return { productId: arr[0], count: parseInt(arr[1]) };
            });
            return React.createElement(React.Fragment, null,
                React.createElement("div", { className: "form-group clearfix" },
                    React.createElement("label", { className: "control-label col-sm-2" }, "*\u8D60\u9001\u5546\u54C1"),
                    React.createElement("div", { className: "col-sm-10" },
                        givenProducts.map((o, i) => React.createElement("div", { key: o.productId, className: "row", style: { marginBottom: 6 } },
                            React.createElement("div", { className: "col-sm-8", style: { paddingRight: 0 } },
                                React.createElement("input", { className: "form-control", readOnly: true, value: objectNames[o.productId] || o.productId })),
                            React.createElement("div", { className: "col-sm-4", style: { paddingLeft: 0 } },
                                React.createElement("div", { className: "input-group" },
                                    React.createElement("select", { className: "form-control", value: o.count, onChange: e => {
                                            let g = givenProducts.filter(c => c.productId == o.productId)[0];
                                            g.count = Number.parseInt(e.target.value);
                                            item.rule.GivenValue = givenProducts.map(o => `${o.productId}:${o.count}`).join(",");
                                            this.setState({ item });
                                        } },
                                        React.createElement("option", { value: "1" }, "1 \u4EF6"),
                                        React.createElement("option", { value: "2" }, "2 \u4EF6"),
                                        React.createElement("option", { value: "3" }, "3 \u4EF6"),
                                        React.createElement("option", { value: "4" }, "4 \u4EF6"),
                                        React.createElement("option", { value: "5" }, "5 \u4EF6"),
                                        React.createElement("option", { value: "6" }, "6 \u4EF6"),
                                        React.createElement("option", { value: "7" }, "7 \u4EF6"),
                                        React.createElement("option", { value: "8" }, "8 \u4EF6"),
                                        React.createElement("option", { value: "9" }, "9 \u4EF6")),
                                    React.createElement("span", { className: "input-group-btn" },
                                        React.createElement("button", { className: "btn btn-default", type: "button", onClick: () => {
                                                givenProducts = givenProducts.filter(c => c.productId != o.productId);
                                                item.rule.GivenValue = givenProducts.map(o => `${o.productId}:${o.count}`).join(",");
                                                this.setState({ item });
                                            } },
                                            React.createElement("i", { className: "icon-minus" }))))))),
                        React.createElement("div", { className: "form-group", style: { paddingTop: 8, paddingBottom: 12 } },
                            React.createElement("div", { className: "input-group" },
                                React.createElement("input", { className: "form-control", readOnly: true, placeholder: "\u8BF7\u9009\u62E9\u5546\u54C1" }),
                                React.createElement("span", { className: "input-group-btn" },
                                    React.createElement("button", { className: "btn btn-default", type: "button", title: "\u70B9\u51FB\u9009\u62E9\u5546\u54C1", onClick: () => {
                                            product_select_dialog_1.ProductSelectDialog.show((items) => {
                                                for (let i = 0; i < items.length; i++) {
                                                    objectNames[items[i].Id] = items[i].Name;
                                                    let p = givenProducts.filter(o => o.productId == items[i].Id)[0];
                                                    if (p)
                                                        continue;
                                                    p = { productId: items[i].Id, count: 1 };
                                                    givenProducts.push(p);
                                                }
                                                item.rule.GivenValue = givenProducts.map(o => `${o.productId}:${o.count}`).join(",");
                                                this.setState({ item });
                                            });
                                        } },
                                        React.createElement("i", { className: "icon-cog" })))),
                            React.createElement("input", { name: this.GIVEN_VALUE, type: "hidden", value: item.rule.GivenValue || "", readOnly: true }),
                            React.createElement("span", { className: `${maishu_dilu_1.FormValidator.errorClassName} ${this.GIVEN_VALUE}`, style: { display: "none" } })))));
        }
    }
    class BuyReduceDialog extends ContentRuleDialog {
        constructor() {
            super(...arguments);
            this.GIVEN_VALUE = "GivenValue";
        }
        componentDidMount() {
            super.componentDidMount();
            this.validator.appendField({ name: this.GIVEN_VALUE, rules: [maishu_dilu_1.rules.numeric("请输入正确的金额")] });
        }
        renderGiven() {
            let { item } = this.state;
            return React.createElement("div", { key: "reduceAmount", className: "form-group" },
                React.createElement("label", { className: "control-label col-sm-2" }, "*\u51CF\u514D\u91D1\u989D"),
                React.createElement("div", { className: "col-sm-10" },
                    React.createElement("div", { className: "input-group" },
                        React.createElement("input", { name: `${this.GIVEN_VALUE}`, className: "form-control", placeholder: "\u8BF7\u8F93\u5165\u51CF\u514D\u91D1\u989D", value: item.rule.GivenValue || "", onChange: e => {
                                item.rule.GivenValue = e.target.value;
                                this.setState({ item });
                            } }),
                        React.createElement("div", { className: "input-group-addon" }, "\u5143")),
                    React.createElement("span", { className: `${maishu_dilu_1.FormValidator.errorClassName} ${this.GIVEN_VALUE}` })));
        }
    }
    class BuyDiscountDialog extends ContentRuleDialog {
        renderGiven() {
            let { rule } = this.state.item;
            let arr = (rule.GivenValue || "").split(".");
            let [pricePercentMain, pricePercentMinor] = arr;
            pricePercentMain = pricePercentMain || "9";
            pricePercentMinor = pricePercentMinor || "0";
            return React.createElement("div", { className: "form-group clearfix" },
                React.createElement("label", { className: "control-label col-sm-2" }, "*\u6253\u6298"),
                React.createElement("div", { className: "col-sm-10" },
                    React.createElement("div", { className: "row" },
                        React.createElement("div", { className: "col-xs-6", style: { paddingRight: 0 } },
                            React.createElement("select", { "data-bind": "value:pricePercentMain", className: "form-control", value: pricePercentMain, onChange: e => {
                                    pricePercentMain = e.target.value;
                                    rule.GivenValue = `${pricePercentMain}.${pricePercentMinor}`;
                                    this.setState(this.state);
                                } },
                                React.createElement("option", { value: "9" }, "9"),
                                React.createElement("option", { value: "8" }, "8"),
                                React.createElement("option", { value: "7" }, "7"),
                                React.createElement("option", { value: "6" }, "6"),
                                React.createElement("option", { value: "5" }, "5"),
                                React.createElement("option", { value: "4" }, "4"),
                                React.createElement("option", { value: "3" }, "3"),
                                React.createElement("option", { value: "2" }, "2"),
                                React.createElement("option", { value: "1" }, "1"))),
                        React.createElement("div", { className: "col-xs-6", style: { paddingLeft: 0 } },
                            React.createElement("div", { className: "input-group" },
                                React.createElement("select", { className: "form-control", value: pricePercentMinor, onChange: e => {
                                        pricePercentMinor = e.target.value;
                                        rule.GivenValue = `${pricePercentMain}.${pricePercentMinor}`;
                                        this.setState(this.state);
                                    } },
                                    React.createElement("option", { value: "0" }, "0"),
                                    React.createElement("option", { value: "1" }, "1"),
                                    React.createElement("option", { value: "2" }, "2"),
                                    React.createElement("option", { value: "3" }, "3"),
                                    React.createElement("option", { value: "4" }, "4"),
                                    React.createElement("option", { value: "5" }, "5"),
                                    React.createElement("option", { value: "6" }, "6"),
                                    React.createElement("option", { value: "7" }, "7"),
                                    React.createElement("option", { value: "8" }, "8"),
                                    React.createElement("option", { value: "9" }, "9")),
                                React.createElement("div", { className: "input-group-addon" }, "\u6298"))))));
        }
    }
});
